home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 1 / Amiga Tools.iso / disk-tools / hd-tools / scsi_tape / source.lha / tape / scsi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-16  |  2.8 KB  |  136 lines

  1.  
  2. /*
  3.  *  SCSI.C
  4.  */
  5.  
  6. #include "defs.h"
  7.  
  8. Prototype int  SCSIOpen(Chan *);
  9. Prototype void SCSIClose(Chan *);
  10. Prototype int  DoSCSI(Chan *, char *, char *, long, ubyte, long *);
  11. Prototype void SendSCSI(Chan *, char *, char *, long, ubyte);
  12. Prototype int  WaitSCSI(Chan *, long *);
  13.  
  14. static __aligned ubyte senseCmd[] = { 0x03, 0x00, 0x00, 0x00, 20, 0 };
  15.  
  16. int
  17. SCSIOpen(chan)
  18. Chan *chan;
  19. {
  20.     int error;
  21.  
  22.     chan->ch_Ios.sr_Req.io_Message.mn_ReplyPort = IoSink;
  23.     chan->ch_Ios.sr_Req.io_Message.mn_Node.ln_Name = (char *)chan;
  24.     if (error = OpenDevice(chan->ch_DeviceName, chan->ch_UnitNo, (IORequest *)&chan->ch_Ios, 0)) {
  25.     chan->ch_Ios.sr_Req.io_Device = NULL;
  26.     error = -1;
  27.     } else {
  28.     DoSCSI(chan, senseCmd, NULL, 0, SCSIF_READ, NULL);
  29.     }
  30.     return(error);
  31. }
  32.  
  33. void
  34. SCSIClose(chan)
  35. Chan *chan;
  36. {
  37.     if (chan->ch_Ios.sr_Req.io_Device) {
  38.     if (chan->ch_Flags & CHANF_IOSIP) {
  39.         AbortIO((IORequest *)&chan->ch_Ios);
  40.         WaitIO((IORequest *)&chan->ch_Ios);
  41.         chan->ch_Flags &= ~CHANF_IOSIP;
  42.     }
  43.     CloseDevice((IORequest *)&chan->ch_Ios);
  44.     chan->ch_Ios.sr_Req.io_Device = NULL;
  45.     }
  46. }
  47.  
  48. int
  49. DoSCSI(chan, cmd, buf, bytes, flags, plen)
  50. Chan *chan;
  51. char *cmd;
  52. char *buf;
  53. long bytes;
  54. ubyte flags;
  55. long *plen;
  56. {
  57.     SendSCSI(chan, cmd, buf, bytes, flags);
  58.     return(WaitSCSI(chan, plen));
  59. }
  60.  
  61. void
  62. SendSCSI(chan, cmd, buf, bytes, flags)
  63. Chan *chan;
  64. char *cmd;
  65. char *buf;
  66. long bytes;
  67. ubyte flags;
  68. {
  69.     SCSIReq *ios = &chan->ch_Ios;
  70.  
  71.     movmem(cmd, chan->ch_Cmd, 6);
  72.  
  73.     ios->sr_Cmd.scsi_Command   = chan->ch_Cmd;
  74.     ios->sr_Cmd.scsi_CmdLength = 6;
  75.     ios->sr_Cmd.scsi_Data   = buf;
  76.     ios->sr_Cmd.scsi_Length = bytes;
  77.     ios->sr_Cmd.scsi_Flags  = flags;
  78.  
  79.     ios->sr_Req.io_Command = 28;
  80.     ios->sr_Req.io_Data    = &ios->sr_Cmd;
  81.     ios->sr_Req.io_Length  = sizeof(ios->sr_Cmd);
  82.  
  83.     SendIO((IORequest *)&ios->sr_Req);
  84.     chan->ch_IosLen = bytes;
  85.     chan->ch_Flags |= CHANF_IOSIP;
  86. }
  87.  
  88. int
  89. WaitSCSI(chan, plen)
  90. Chan *chan;
  91. long *plen;
  92. {
  93.     SCSIReq *ios = &chan->ch_Ios;
  94.     ubyte senseBuf[20];
  95.     int error;
  96.  
  97.     if (chan->ch_Flags & CHANF_IOSIP) {
  98.     WaitIO((IORequest *)&ios->sr_Req);
  99.     chan->ch_Flags &= ~CHANF_IOSIP;
  100.     }
  101.  
  102.     if (plen)
  103.     *plen = ios->sr_Cmd.scsi_Actual;
  104.  
  105.     switch (error = ios->sr_Req.io_Error) {
  106.     case 0:
  107.     break;
  108.     case HFERR_BadStatus:
  109.     ios->sr_Cmd.scsi_Command   = senseCmd;
  110.     ios->sr_Cmd.scsi_CmdLength = 6;
  111.     ios->sr_Cmd.scsi_Data    = (UWORD *)senseBuf;
  112.     ios->sr_Cmd.scsi_Length = sizeof(senseBuf);
  113.     ios->sr_Cmd.scsi_Flags    = SCSIF_READ;
  114.  
  115.     if (DoIO((IORequest *)&ios->sr_Req)) {
  116.         error = -256;
  117.         break;
  118.     }
  119.     if (ios->sr_Cmd.scsi_Actual < 4) {
  120.         error = -257;
  121.         break;
  122.     }
  123.     error = senseBuf[2];
  124.     if (error == SENSE_RECOVERED_ERROR && ios->sr_Cmd.scsi_Actual == chan->ch_IosLen)
  125.         error = 0;
  126.     break;
  127.     default:
  128.     error = -error;
  129.     break;
  130.     }
  131.     if (error)
  132.     printf("SCSI CMD %d ERROR %d\n", chan->ch_Cmd[0], error);
  133.     return(error);
  134. }
  135.  
  136.